package znet

import (
	"fmt"
	"google.golang.org/protobuf/proto"
	"svvenus/zlog"
	"svvenus/znet/zpack"
	"svvenus/znet/zpb"
	"svvenus/znet/ztcp"
)

type Request struct {
	Conn *ztcp.TcpConnection //已经和客户端建立好的 链接
	Msg  *zpack.PackBuffer   //客户端请求的数据
}

//获取请求连接信息
func (r *Request) GetConnection() *ztcp.TcpConnection {
	return r.Conn
}

//获取请求消息的数据
func (r *Request) GetData() []byte {
	return r.Msg.Data
}

//获取请求的消息的ID
func (r *Request) GetMsgID() uint16 {
	return r.Msg.Id
}

//获取请求的消息的Code
func (r *Request) GetMsgCode() uint16 {
	return r.Msg.Code
}

//解析成pb消息
func (r *Request) GetPb(msg proto.Message) bool {
	if err := proto.Unmarshal(r.GetData(), msg); err != nil {
		fmt.Println(err)
		return false
	}
	return true
}

//发送pb消息
func (r *Request) SendId(msgId zpb.EPid) {
	zlog.Debug(fmt.Sprintf("SendId : msgId=%d", msgId))
	r.GetConnection().SendId(msgId)
}

//发送pb消息
func (r *Request) SendCode(msgId zpb.EPid, code uint16) {
	zlog.Debug(fmt.Sprintf("SendId : msgId=%d code=%d", msgId, code))
	r.GetConnection().SendCode(msgId, code)
}

//发送pb消息
func (r *Request) Send(msgId zpb.EPid, code uint16, msg proto.Message) {
	zlog.Debug(fmt.Sprintf("SendId : msgId=%d code=%d ", msgId, code), msg)
	r.GetConnection().Send(msgId, code, msg)
}
